package com.woniuxy.jiapei.controller;


import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.woniuxy.jiapei.exception.MyException;
import com.woniuxy.jiapei.pojo.Area;
import com.woniuxy.jiapei.service.AreaService;
import com.woniuxy.jiapei.util.R;
import com.woniuxy.jiapei.util.ResultCode;
import com.woniuxy.jiapei.util.ResultMsg;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.List;

/**
 * <p>
 * 前端控制器
 * </p>
 *
 * @author lr
 * @since 2020-10-21
 */
@RestController
@RequestMapping("/jiapei/area")
@CrossOrigin
public class AreaController {
    @Autowired
    private AreaService areaService;

    @GetMapping("/provinces")
    @ApiOperation("获取省份信息")
    public R getProvinces() throws MyException {
        QueryWrapper queryWrapper = new QueryWrapper();

        queryWrapper.eq("pid", 0);
        List<Area> provinces = areaService.list(queryWrapper);
        if (provinces != null && provinces.size() > 0) {
            return R.ok().data("provinces", provinces);
        } else {
            throw new MyException(ResultCode.GETPROVINCEFAILED, ResultMsg.GETPROVINCEFAILEDMSG);
        }
    }

    @GetMapping("/cities/{sid}")
    @ApiOperation("获取市信息")
    public R getCities(@PathVariable Integer sid) throws MyException {
        QueryWrapper queryWrapper = new QueryWrapper();

        queryWrapper.eq("pid", sid);
        List<Area> cities = areaService.list(queryWrapper);
        if (cities != null && cities.size() > 0) {
            return R.ok().data("cities", cities);
        } else {
            throw new MyException(ResultCode.GETCITYFAILED, ResultMsg.GETCITYFAILEDMSG);
        }
    }

    @GetMapping("/districts/{cid}")
    @ApiOperation("获取区信息")
    public R getDistricts(@PathVariable Integer cid) throws MyException {
        QueryWrapper queryWrapper = new QueryWrapper();

        queryWrapper.eq("pid", cid);
        List<Area> districts = areaService.list(queryWrapper);
        if (districts != null && districts.size() > 0) {
            return R.ok().data("districts", districts);
        } else {
            throw new MyException(ResultCode.GETDISTRICTFAILED, ResultMsg.GETDISTRICTFAILEDMSG);
        }
    }
    @GetMapping("/counties/{countyId}")
    @ApiOperation("获取县信息")
    public R getCounties(@PathVariable Integer countyId) throws MyException {
        QueryWrapper queryWrapper = new QueryWrapper();

        queryWrapper.eq("pid", countyId);
        List<Area> counties = areaService.list(queryWrapper);
        if (counties != null && counties.size() > 0) {
            return R.ok().data("counties", counties);
        } else {
            throw new MyException(ResultCode.GETCOUNTYFAILED, ResultMsg.GETCOUNTYFAILEDMSG);
        }
    }
}

